Grokking JavaScript Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who should take this course?
Course Overview
Introduction to JavaScript
JavaScript Basics
JavaScript - Hello World
JavaScript - Comments
Quiz
JavaScript - Variables
JavaScript - Operators
Quiz
Introduction to Conditional Statements in JavaScript
JavaScript - If...Else
JavaScript - Switch Case
Quiz
Introduction to Objects in JavaScript
JavaScript - Number
JavaScript - Boolean
JavaScript - Math
JavaScript - Date
Quiz
Introductions to Function in JavaScript
JavaScript - Variable Scope
JavaScript - Default Parameters
JavaScript - Arrow Functions
Quiz
Introduction to Strings in JavaScript
JavaScript - String Methods
Introduction to Array in JavaScript
JavaScript - Array Methods
Quiz
Introduction to loops in JavaScript
JavaScript - for loop
JavaScript - while loop
JavaScript - do-while loop
JavaScript - for-in loop
JavaScript - for-of loop
JavaScript - Loop Control
Quiz
JavaScript - Maps
JavaScript - Sets
Quiz
Where to Go from Here
JavaScript - Comments

Comments are an essential part of programming in any language, including JavaScript. They allow developers to write notes within their code, explaining what certain parts of the code do, marking sections, or even temporarily disabling code that shouldn't be executed. Comments are ignored by the JavaScript engine, meaning they don't affect the execution of the program in any way.

Types of Comments in JavaScript

JavaScript supports two main types of comments:

  1. Single-line Comments: Begin with two forward slashes // and extend to the end of the line.
  2. Multi-line Comments: Start with /* and end with */. Everything between these two symbols is commented out, including line breaks.

Single-line Comments

Single-line comments are often used to explain a line of code or to temporarily disable code for debugging purposes. Here's an example that demonstrates both uses:

Javascript

. . . .
  • The first console.log statement is preceded by a comment that explains what the line does. When executed, this line prints Hello World! to console.
  • The second console.log statement is disabled by a single-line comment. It's a way to temporarily remove this line from execution without deleting it. If you remove the //, this line will also execute, printing the text to the console.

Multi-line Comments

Multi-line comments are useful for providing detailed explanations or for commenting out blocks of code. Here's an example that showcases both aspects:

Javascript

. . . .
  • The initial block comment provides an overview of the purpose behind the following code snippet, which is to print two messages on the console.
  • The final block comment demonstrates how multiple lines of code can be commented out, which in this scenario includes two console.log statements. These lines are effectively disabled and will not execute, showing how multi-line comments can be used to temporarily remove code from execution.

Best Practices for Using Comments

  • Clarity: Write clear and concise comments that explain the "why" behind your code, not just the "what".
  • Maintenance: Keep comments updated as you modify your code to ensure they remain relevant and helpful.
  • Moderation: Use comments judiciously. Excessive commenting can make code cluttered and difficult to read. Aim to write self-explanatory code and comment only when necessary.
JavaScript - Hello World
Quiz
Mark as Completed

On this page

Types of Comments in JavaScript

Single-line Comments

Multi-line Comments

Best Practices for Using Comments